import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class HomeHeaderDelegate extends SliverPersistentHeaderDelegate {
  HomeHeaderDelegate({
    required this.expandedHeight,
    required this.shrinkHeight,
  });
  // 展开高度
  final double expandedHeight;
  // 收缩高度
  final double shrinkHeight;

  @override
  double get maxExtent => expandedHeight;

  @override
  double get minExtent => shrinkHeight;

  @override
  bool shouldRebuild(covariant HomeHeaderDelegate oldDelegate) {
    return expandedHeight != oldDelegate.expandedHeight;
  }

  @override
  Widget build(
    BuildContext context,
    double shrinkOffset,
    bool overlapsContent,
  ) {
    // 更新计算结果
    final obj = updateHeaderCalculations(context, shrinkOffset);
    return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [Color(0xFF1A82F5), Color(0xFF1563E7)],
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
        ),
      ),
      child: Stack(
        fit: StackFit.expand,
        children: [
          // 标题
          Positioned(
            left: 16.w,
            top: -shrinkOffset,
            height: 44.h,
            child: Center(
              child: Opacity(
                opacity: obj.titleOpacity,
                child: Text(
                  '聚名网',
                  style: TextStyle(fontSize: 18.sp, color: Colors.white),
                ),
              ),
            ),
          ),
          // 功能按钮区域：固定在右上角
          Positioned(
            right: 16.w,
            top: 0,
            height: 44.h,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                IconButton(
                  icon: Icon(Icons.support_agent, color: Colors.white),
                  onPressed: () {},
                ),
                IconButton(
                  icon: Icon(Icons.add, color: Colors.white),
                  onPressed: () {},
                ),
              ],
            ),
          ),
          // 搜索框部分：根据滚动调整位置和宽度
          Positioned(
            left: 16.w,
            top: obj.searchBoxTop,
            child: Container(
              width: obj.searchWidth,
              height: obj.searchHeight,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
              ),
              child: TextField(
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.search, color: Colors.grey),
                  hintText: "查交易、whois、注册/功能",
                  border: InputBorder.none,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  ({
    double titleOpacity,
    double searchWidth,
    double searchHeight,
    double searchBoxTop,
  })
  updateHeaderCalculations(BuildContext context, double shrinkOffset) {
    // 收缩范围
    final double fadeRange = expandedHeight - shrinkHeight;
    // 标题的不透明度
    final double titleOpacity = ((fadeRange - shrinkOffset) / fadeRange).clamp(
      0.0,
      1.0,
    );
    // 上移进度
    final double progress = (shrinkOffset / fadeRange).clamp(0.0, 1.0);

    // 搜索框宽度：》screenwidth - 32.w =》screenwidth - 132.w
    // 搜索框高度：40.h =》32.h
    final double fullSearchWidth = MediaQuery.of(context).size.width - 32.w;
    final double buttonsWidth = 100.0.w;
    final double searchWidth = fullSearchWidth - buttonsWidth * progress;
    final double fullSearchHeight = 40.h;
    final double searchHeight = fullSearchHeight - 8.h * progress;
    final double searchStartTop = expandedHeight - searchHeight - 16.h;
    final double searchEndTop = 6.h;
    final double searchBoxTop =
        searchStartTop - (searchStartTop - searchEndTop) * progress;

    return (
      titleOpacity: titleOpacity,
      searchWidth: searchWidth,
      searchHeight: searchHeight,
      searchBoxTop: searchBoxTop,
    );
  }
}
